package org.lai.service.impl;

import org.apache.ibatis.annotations.Mapper;
import org.lai.Exception.haveStudentException;
import org.lai.mapper.DeptMapper;
import org.lai.pojo.Dept;
import org.lai.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service  // 将DeptServiceImpl类注入到spring容器中
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {

        return deptMapper.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        Integer count = deptMapper.getCountById(id);
        if(count > 0){
            throw new haveStudentException("该部门下有员工,不能删除");
        }
        deptMapper.deleteById(id);
    }

    @Override
    public void add(Dept dept) {
        // 1.补全基础属性 -createTime,updateTime
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        // 2.调用mapper接口操作数据
        deptMapper.insert(dept);
    }
    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }
    @Override
    public void update(Dept dept) {
        // 1.补全基础属性 -updateTime
        dept.setUpdateTime(LocalDateTime.now());
        // 2.调用mapper接口操作数据
        deptMapper.update(dept);
    }
}
